
function dbname() {
	var match = /\/_(\w+)\//.exec(window.location);
    var db_name = match[1];
	return db_name;
}


function wikiId() {
    var match = /\?(\w+)/.exec(window.location);
	if (match === null || match.length == 0) return "";
	var db_name = match[1];
	return db_name;
}
function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
   return (S4()+S4()+""+S4()+""+S4()+""+S4()+""+S4()+S4()+S4());
}
function anchor() {
	return self.document.location.hash;
}


var zeropad = function(num) {
	return ((num < 10) ? '0' : '') + num;
}

var iso8601 = function (date) {
	return date.getUTCFullYear() 
	 + "-" + zeropad(date.getUTCMonth()+1)
	 + "-" + zeropad(date.getUTCDate())
	 + "T" + zeropad(date.getUTCHours())
	 + ":" + zeropad(date.getUTCMinutes())
	 + ":" + zeropad(date.getUTCSeconds()) + "Z";
}

function findRecentByDate(db) {
	var results = db.view("system/changes_by_date", {limit : 20, descending:true});
	return results.rows;
}

function findTagsDoc(tags, db) {

	var result = db.view("system/tag_to_schema",{},tags);
	return result.rows;
}




     function _renderValue(value) {
        function isNullOrEmpty(val) {
          if (val == null) return true;
          for (var i in val) return false;
          return true;
        }
        function render(val) {
          var type = typeof(val);
          if (type == "object" && !isNullOrEmpty(val)) {
            var list = $("<dl></dl>");
            for (var i in val) {
              $("<dt></dt>").text(i).appendTo(list);
              $("<dd></dd>").append(render(val[i])).appendTo(list);
            }
            return list;
          } else {
            var html = $.futon.formatJSON(val, {
              html: true,
              escapeStrings: false
            });
            return $(html);
          }
        }
        var elem = render(value);

        elem.find("dd:has(dl)").hide().prev("dt").addClass("collapsed");
        elem.find("dd:not(:has(dl))").addClass("inline").prev().addClass("inline");
        elem.find("dt.collapsed").click(function() {
          $(this).toggleClass("collapsed").next().toggle();
        });

        return elem;
      }

